/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.easoftware.code.beans.prime;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import net.easoftware.code.beans.BeanHelper;
import net.easoftware.code.beans.facade.PerfilDatosFc;
import net.easoftware.code.mb.MbPerfilesDatos;

/**
 *
 * @author eliu
 */
@FacesConverter( "perfilDatosConverter")
public class PerfilDatosConverter implements Converter, Serializable {

    private static List<PerfilDatosFc> perfiles = new LinkedList<>();

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        if (perfiles.isEmpty()) {
            MbPerfilesDatos mb = (MbPerfilesDatos) BeanHelper.findBean("mbPerfilesDatos");
            perfiles = mb.getLista();
        }

        for (PerfilDatosFc ln : perfiles) {
            if (ln.getDescripcion().equalsIgnoreCase(value)) {
                return ln;
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        return value instanceof PerfilDatosFc ? ( (PerfilDatosFc) value ).getDescripcion() : "";
    }
}
